package server

import (
	v1 "phm/api/helloworld/v1"
	pb2 "phm/api/phm/v1"
	pb "phm/api/phm/v1/bigscreen"
	pw "phm/api/phm/v1/webmanage"
	"phm/internal/conf"
	"phm/internal/service"
	"phm/internal/service/bigscreen"
	"phm/internal/service/webmanage"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/transport/grpc"
)

// NewGRPCServer new a gRPC server.
func NewGRPCServer(
	greeter *service.GreeterService,
	radarService *service.RadarService,
	studentService *service.StudentService,
	userService *webmanage.UserService,
	roleService *webmanage.RoleService,
	operationService *webmanage.OperationService,
	menuService *webmanage.PermissionMenuService,
	bigscreenService *bigscreen.RadarBigscreenService,
	logger log.Logger) *grpc.Server {
	var opts = []grpc.ServerOption{
		grpc.Middleware(
			recovery.Recovery(),
		),
	}
	if conf.Config.Server.Grpc.Network != "" {
		opts = append(opts, grpc.Network(conf.Config.Server.Grpc.Network))
	}
	if conf.Config.Server.Grpc.Addr != "" {
		opts = append(opts, grpc.Address(conf.Config.Server.Grpc.Addr))
	}
	if conf.Config.Server.Grpc.Timeout != nil {
		opts = append(opts, grpc.Timeout(conf.Config.Server.Grpc.Timeout.AsDuration()))
	}
	srv := grpc.NewServer(opts...)
	pw.RegisterUserServer(srv, userService)
	pw.RegisterOperationServer(srv, operationService)
	pw.RegisterRoleServer(srv, roleService)
	pw.RegisterPermissionMenuServer(srv, menuService)
	//
	pb.RegisterRadarBigscreenServer(srv, bigscreenService)
	//
	pb2.RegisterRadarServer(srv, radarService)
	pb2.RegisterStudentServer(srv, studentService)
	v1.RegisterGreeterServer(srv, greeter)
	return srv
}
